/*
 * @Author: your name
 * @Date: 2021-11-03 17:11:23
 * @LastEditTime: 2021-11-04 17:30:37
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \react-qunaer\src\index\store\actions.js
 */
export const SET_FROM = 'SET_FROM'
export const SET_TO = 'SET_TO'
export const SET_IS_CITY_SELECTOR_VISIBLE = 'SET_IS_CITY_SELECTOR_VISIBLE'
export const SET_CURRENT_SELECTING_LEFT_CITY = 'SET_CURRENT_SELECTING_LEFT_CITY'
export const SET_CITY_DATA = 'SET_CITY_DATA'
export const SET_IS_LOADING_CITY_DATA = 'SET_IS_LOADING_CITY_DATA'
export const SET_IS_DATE_SELECTOR_VISIBLE = 'SET_IS_DATE_SELECTOR_VISIBLE'
export const SET_HIGH_SPEED = 'SET_HIGH_SPEED'
export const SET_DEPART_DATE = 'SET_DEPART_DATE'

export const setFrom = data => ({type: SET_FROM, data})
export const setTo = data => ({type: SET_TO, data})
export const setIsLoadingCityData = data => ({type: SET_IS_LOADING_CITY_DATA, data})
export const setCityData = data => ({type: SET_CITY_DATA, data})
// 是否只看高铁动车
export const toggleHighSpeed = () => {
  return (dispatch, getState) => {
    const {highSpeed} = getState()
    dispatch({type: SET_HIGH_SPEED, data: !highSpeed})
  }
}
export const showCitySelector = data => ({type: SET_IS_CITY_SELECTOR_VISIBLE, data})
export const hideCitySelector = data => ({type: SET_IS_CITY_SELECTOR_VISIBLE, data})
export const hideDateSelector = () => ({type: SET_IS_DATE_SELECTOR_VISIBLE, data: false})
// 点击选择的城市
export const setSelectedCity = city => {
  return (dispatch, getState) => {
    const {currentSelectingLeftCity} = getState();
    if (currentSelectingLeftCity) dispatch(setFrom(city));
    else dispatch(setTo(city))
    dispatch(hideCitySelector(false))
  }
}
export const showDateSelector = () => ({type: SET_IS_DATE_SELECTOR_VISIBLE, data: true})
// 始发地、终点地切换
export const exchangeFromTo = () => {
  return (dispatch, getState) => {
    const {from, to} = getState()
    dispatch(setFrom(to))
    dispatch(setTo(from))
  }
}
export const setDepartDate = data => ({type: SET_DEPART_DATE, data})
// 模拟获取城市请求
export const fetchCityData = () => {
  return (dispatch, getState) => {
    const {isLoadingCityData} = getState()
    const cityData = require('./cityData.json')
    if (isLoadingCityData) return
    dispatch(setIsLoadingCityData(true))
    // http://mr1024.hl98.cn/MrLiang_citys.txt
    localStorage.setItem('cityData', JSON.stringify(cityData))
    dispatch(setCityData(cityData))
    dispatch(setIsLoadingCityData(false))
  }
} 